package com.dbq.fast.business.main.model;

import com.dbq.fast.App;
import com.dbq.fast.BuildConfig;
import com.dbq.fast.business.main.bean.SubscriptionMeal;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.business.product.bean.GoogleReturnOrder;
import com.dbq.fast.common.bean.User;
import com.dbq.fast.common.json.GsonT;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.common.net.RemoteApiCallBack;
import com.dbq.fast.util.SystemUtil;
import com.yiba.vpn.utils.Constants;
import com.yiba.vpn.utils.RxBus;

import java.util.HashMap;

/**
 * Created by yuanchen on 2018/4/13.
 */

public class FlashSaleModel {

    private static HttpManager httpManager = HttpManager.getInstance();

    public static void querySubscriptionMeal(RemoteApiCallBack<SubscriptionMeal> c){
        httpManager.getHttpService()
                .querySubscriptionMeal()
                .compose(new CommonTransformer<>())
                .subscribe(listHttpResult -> {
                    if (listHttpResult.isSuccess()) {
                        c.onSuccess(listHttpResult.getData());
                    }else {
                        c.onFail(listHttpResult.getCode(), "");
                    }
                }, throwable -> {
                    c.onFail(-1, "");
                });
    }

    public static void submitOrder(String purchaseData, String dataSignature, int mealId){
        String token = User.getInstance().getToken();
        GoogleReturnOrder gro = GsonT.parseJson(purchaseData, GoogleReturnOrder.class);
        HashMap data = new HashMap();
        data.put("account", User.getInstance().getAccount());
        data.put("dataSignature", dataSignature);
        data.put("subscriptionId", gro.productId);
        data.put("isRenew", 0);
        data.put("payPlatform", "0");
        data.put("country", SystemUtil.getCountry(App.getContext()));
        data.put("orderId", gro.orderId);
        data.put("purchaseState", gro.purchaseState);
        data.put("purchaseToken", gro.purchaseToken);
        data.put("packageName", App.getContext().getPackageName());
        data.put("setMealId", mealId);
        data.put("versionName", BuildConfig.VERSION_NAME);
        httpManager.getHttpService()
                .submitSubsOrder(token, data)
                .retry(3)
                .compose(new CommonTransformer<>())
                .subscribe(doUserDiscountHttpResult -> {
                    if (doUserDiscountHttpResult.isSuccess()) {
                        RxBus.get().post(Constants.RXBUS_SUBSCRIBE_SUCCESS_DIALOG, true);
                        RxBus.get().post(Constants.RXBUS_SUBSCRIBE_SUCCESS_BANNER, true);
                    }
                }, throwable -> {

                });
    }

}
